package com.ruoyi.t.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.t.domain.TOrder;
import com.ruoyi.t.domain.TStyle;
import com.ruoyi.t.mapper.TOrderMapper;
import com.ruoyi.t.mapper.TStyleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.t.mapper.TOrderClientPaymentMapper;
import com.ruoyi.t.domain.TOrderClientPayment;
import com.ruoyi.t.service.ITOrderClientPaymentService;

/**
 * 订单客户收款Service业务层处理
 *
 * @author YY
 * @date 2025-02-06
 */
@Service
public class TOrderClientPaymentServiceImpl implements ITOrderClientPaymentService
{
    @Autowired
    private TOrderClientPaymentMapper tOrderClientPaymentMapper;

    @Autowired
    private TOrderMapper tOrderMapper;

    @Autowired
    private TStyleMapper tStyleMapper;

    /**
     * 查询订单客户收款
     *
     * @param id 订单客户收款主键
     * @return 订单客户收款
     */
    @Override
    public TOrderClientPayment selectTOrderClientPaymentById(Long id)
    {
        return tOrderClientPaymentMapper.selectTOrderClientPaymentById(id);
    }

    /**
     * 查询订单客户收款列表
     *
     * @param tOrderClientPayment 订单客户收款
     * @return 订单客户收款
     */
    @Override
    public List<TOrderClientPayment> selectTOrderClientPaymentList(TOrderClientPayment tOrderClientPayment)
    {
        return tOrderClientPaymentMapper.selectTOrderClientPaymentList(tOrderClientPayment);
    }

    /**
     * 新增订单客户收款
     *
     * @param tOrderClientPayment 订单客户收款
     * @return 结果
     */
    @Override
    public int insertTOrderClientPayment(TOrderClientPayment tOrderClientPayment)
    {
        tOrderClientPayment.setCreateTime(DateUtils.getNowDate());

        TOrder tOrder = tOrderMapper.selectTOrderById(tOrderClientPayment.getOrderId());

        if(tOrderClientPayment.getStyleId()!=null){
            TStyle tStyle = tStyleMapper.selectTStyleById(tOrderClientPayment.getStyleId());
            tOrderClientPayment.setStyleName(tStyle.getStyleName());
        }

        BigDecimal balance = tOrder.getBalance();
        if(tOrderClientPayment.getPrice().compareTo(balance) > 0){
            throw new ServiceException("超出设置的订单余额");
        }
        balance = balance.subtract(tOrderClientPayment.getPrice());

        tOrder.setBalance(balance);
        tOrderMapper.updateTOrder(tOrder);

        return tOrderClientPaymentMapper.insertTOrderClientPayment(tOrderClientPayment);
    }

    /**
     * 修改订单客户收款
     *
     * @param tOrderClientPayment 订单客户收款
     * @return 结果
     */
    @Override
    public int updateTOrderClientPayment(TOrderClientPayment tOrderClientPayment)
    {
        tOrderClientPayment.setUpdateTime(DateUtils.getNowDate());

        TOrder tOrder = tOrderMapper.selectTOrderById(tOrderClientPayment.getOrderId());

        TOrderClientPayment tOrderClientPayment1 = tOrderClientPaymentMapper.selectTOrderClientPaymentById(tOrderClientPayment.getId());

        BigDecimal balance = tOrder.getBalance();
        balance = balance.add(tOrderClientPayment1.getPrice());
        if(tOrderClientPayment.getPrice().compareTo(balance) > 0){
            throw new ServiceException("超出设置的订单余额");
        }
        balance = balance.subtract(tOrderClientPayment.getPrice());

        tOrder.setBalance(balance);
        tOrderMapper.updateTOrder(tOrder);

        return tOrderClientPaymentMapper.updateTOrderClientPayment(tOrderClientPayment);
    }

    /**
     * 批量删除订单客户收款
     *
     * @param ids 需要删除的订单客户收款主键
     * @return 结果
     */
    @Override
    public int deleteTOrderClientPaymentByIds(Long[] ids)
    {
        return tOrderClientPaymentMapper.deleteTOrderClientPaymentByIds(ids);
    }

    /**
     * 删除订单客户收款信息
     *
     * @param id 订单客户收款主键
     * @return 结果
     */
    @Override
    public int deleteTOrderClientPaymentById(Long id)
    {
        return tOrderClientPaymentMapper.deleteTOrderClientPaymentById(id);
    }
}
